﻿using System;
using System.Collections.Generic;
using System.Xml;
using Autodesk.Revit.DB;
using YArchitech.Revit;
using YJKOpeningCommon.DataEnum;
using YJKOpeningCommon.OpeningEX;
using YJKOpeningCommon.OpeningProfile;
using YJKOpeningCommon.Public;
using YJKOpeningCreate.Public;
using YJKRGeometry;

namespace YJKOpeningCommon.DbOpening
{
	public class DbCompositeRectFloorOpening : DbCompositeOpening
	{
		public override bool CreateOpeningFamilyInstance(ViewScene ViewScene, out Element elem3D, out Element elem2D)
		{
			elem2D = null;
			elem3D = null;
			View viewOpening2D = ViewScene.m_viewOpening2D;
			if (viewOpening2D == null)
			{
				elem3D = null;
				return false;
			}
			Document document = viewOpening2D.Document;
			OpeningXData openingXData = new OpeningXData();
			base.GetOpeningXDataBase(ref openingXData);
			Transaction transaction = new Transaction(document, "CreateOpeningFamilyInstance");
			try
			{
				FamilySymbolOperator familySymbolOperator = new FamilySymbolOperator(document, this);
				if (!familySymbolOperator.DoCalculate())
				{
					return false;
				}
				FamilySymbol familySymble = familySymbolOperator.GetFamilySymble();
				transaction.Start();
				OpeningProfileRect openingProfileRect = this.m_profile as OpeningProfileRect;
				Element element = document.GetElement(this.m_strHostUniqueId);
				Floor floor = element as Floor;
				Level level = document.GetElement(floor.LevelId) as Level;
				UnifiedModified.ActivateFamilySymbol(familySymble);
				FamilyInstance familyInstance = document.Create.NewFamilyInstance(openingProfileRect.m_position, familySymble, floor, level, 0);
				double num = 0.0;
				double num2 = 0.0;
				if (familySymbolOperator.IsNeedSetSize())
				{
					num = openingProfileRect.m_dExtendDist * 2.0 + openingProfileRect.m_cutHeight;
					num2 = openingProfileRect.m_dExtendDist * 2.0 + openingProfileRect.m_cutWidth;
					familyInstance.GetParameter("洞口高度").Set(num);
					familyInstance.GetParameter("洞口宽度").Set(num2);
				}
				XYZ xyz = openingProfileRect.m_position + XYZ.BasisZ;
				Line line = Line.CreateBound(xyz, openingProfileRect.m_position);
				ElementTransformUtils.RotateElement(document, familyInstance.Id, line, openingProfileRect.m_angle);
				openingXData.m_dHeight = num;
				openingXData.m_dWidth = num2;
				openingXData.m_eOpeningProfile = 1;
				openingXData.WriteOpeningXData(familyInstance);
				transaction.Commit();
				elem3D = familyInstance;
			}
			catch (Exception)
			{
				elem3D = null;
				transaction.RollBack();
				return false;
			}
			return true;
		}

		public override bool LoadOpeningFamily(Document doc, ref FamilySymbol familySymbol)
		{
			base.SetFamilyName("楼板洞口-矩形");
			return base.LoadOpeningFamily(doc, ref familySymbol);
		}

		public override bool IsNeedCreateFamily()
		{
			return false;
		}

		public override bool CreateOpeningPreviewInstance(out Element elemPreview, PreViewScene preViewScene)
		{
			OpeningProfileRect openingProfileRect = this.m_profile as OpeningProfileRect;
			XYZ position = openingProfileRect.m_position;
			XYZ basisZ = XYZ.BasisZ;
			XYZ basisY = XYZ.BasisY;
			bool flag = false;
			Transaction transaction = new Transaction(preViewScene.m_doc, "CreateModelGroup");
			bool result;
			try
			{
				XYZ xyz = position + XYZ.BasisZ * preViewScene.m_offset;
				XYZ xyz2 = xyz + XYZ.BasisX.Negate() * (openingProfileRect.m_cutWidth / 2.0 + openingProfileRect.m_dExtendDist) + XYZ.BasisY.Negate() * (openingProfileRect.m_cutHeight / 2.0 + openingProfileRect.m_dExtendDist);
				XYZ xyz3 = xyz2 + XYZ.BasisX * (openingProfileRect.m_cutWidth + openingProfileRect.m_dExtendDist * 2.0);
				XYZ item = xyz3 + XYZ.BasisY * (openingProfileRect.m_cutHeight + openingProfileRect.m_dExtendDist * 2.0);
				XYZ item2 = xyz2 + XYZ.BasisY * (openingProfileRect.m_cutHeight + openingProfileRect.m_dExtendDist * 2.0);
				List<ElementId> list = new List<ElementId>();
				List<XYZ> list2 = new List<XYZ>();
				list2.Add(xyz2);
				list2.Add(xyz3);
				list2.Add(item);
				list2.Add(item2);
				SketchPlane sketchPlane = null;
				if (!CommonInterface.CreateSketchPlane(preViewScene.m_doc, XYZ.BasisZ, xyz, ref sketchPlane) || sketchPlane == null)
				{
					elemPreview = null;
					result = false;
				}
				else if (!CommonInterface.CreateModelLine(list2, preViewScene.m_doc, sketchPlane, ref list, preViewScene.m_modelLineElemId))
				{
					elemPreview = null;
					result = false;
				}
				else
				{
					XYZ xyz4 = position + XYZ.BasisZ.Negate() * (preViewScene.m_offset + this.m_dHostWidth);
					XYZ xyz5 = xyz4 + XYZ.BasisX.Negate() * (openingProfileRect.m_cutWidth / 2.0 + openingProfileRect.m_dExtendDist) + XYZ.BasisY.Negate() * (openingProfileRect.m_cutHeight / 2.0 + openingProfileRect.m_dExtendDist);
					XYZ xyz6 = xyz5 + XYZ.BasisX * (openingProfileRect.m_cutWidth + openingProfileRect.m_dExtendDist * 2.0);
					XYZ item3 = xyz6 + XYZ.BasisY * (openingProfileRect.m_cutHeight + openingProfileRect.m_dExtendDist * 2.0);
					XYZ item4 = xyz5 + XYZ.BasisY * (openingProfileRect.m_cutHeight + openingProfileRect.m_dExtendDist * 2.0);
					list2.Clear();
					list2.Add(xyz5);
					list2.Add(xyz6);
					list2.Add(item3);
					list2.Add(item4);
					sketchPlane = null;
					if (!CommonInterface.CreateSketchPlane(preViewScene.m_doc, XYZ.BasisZ, xyz4, ref sketchPlane) || sketchPlane == null)
					{
						elemPreview = null;
						result = false;
					}
					else if (!CommonInterface.CreateModelLine(list2, preViewScene.m_doc, sketchPlane, ref list, preViewScene.m_modelLineElemId))
					{
						elemPreview = null;
						result = false;
					}
					else
					{
						transaction.Start();
						flag = true;
						elemPreview = preViewScene.m_doc.Create.NewGroup(list);
						transaction.Commit();
						result = true;
					}
				}
			}
			catch (Exception)
			{
				if (flag)
				{
					transaction.RollBack();
				}
				elemPreview = null;
				result = false;
			}
			return result;
		}

		public override void AddSharedParameters(Element elem)
		{
			Document document = elem.Document;
			Transaction transaction = new Transaction(document, "AddSharedParameter");
			try
			{
				transaction.Start();
				base.AddSharedParameters(elem);
				Parameter parameter = elem.GetParameter(DbOpeningBase.m_strSharedParaOpeningMajor);
				if (parameter != null && StrSpecialityObj.m_strSpecialityObj.GetLength(0) > (int)this.m_eSpeciality)
				{
					parameter.Set(StrSpecialityObj.m_strSpecialityObj[(int)this.m_eSpeciality]);
				}
				parameter = elem.GetParameter(DbOpeningBase.m_strSharedParaOpeningSize);
				if (parameter != null)
				{
					string text = "φ" + this.GetOpeningSize(false);
					parameter.Set(text);
				}
				transaction.Commit();
			}
			catch
			{
				transaction.RollBack();
			}
		}

		public override bool Equals(object obj)
		{
			return base.Equals(obj);
		}

		public override int GetHashCode()
		{
			return base.GetHashCode();
		}

		public override bool Export2XmlNode(XmlElement xmlNode)
		{
			bool result;
			try
			{
				if (!base.Export2XmlNode(xmlNode))
				{
					result = false;
				}
				else
				{
					XmlDocument ownerDocument = xmlNode.OwnerDocument;
					foreach (DbOpeningBase dbOpeningBase in this.m_lstDbFamilyOpening)
					{
						XmlElement xmlElement = ownerDocument.CreateElement("DataItem");
						if (dbOpeningBase.Export2XmlNode(xmlElement))
						{
							xmlNode.AppendChild(xmlElement);
						}
					}
					result = true;
				}
			}
			catch
			{
				xmlNode = null;
				result = false;
			}
			return result;
		}

		public override bool ImportByXmlNode(XmlNode xmlNode)
		{
			try
			{
				if (!base.ImportByXmlNode(xmlNode))
				{
					return false;
				}
			}
			catch
			{
				return false;
			}
			return true;
		}

		public override void GetCompositeOpeningData(List<XYZ> lstPt, Document doc)
		{
			double num = double.MaxValue;
			double num2 = double.MaxValue;
			double num3 = double.MinValue;
			double num4 = double.MinValue;
			foreach (XYZ xyz in lstPt)
			{
				if (Geometry.LessThan(xyz.X, num))
				{
					num = xyz.X;
				}
				if (Geometry.LessThan(num3, xyz.X))
				{
					num3 = xyz.X;
				}
				if (Geometry.LessThan(xyz.Y, num2))
				{
					num2 = xyz.Y;
				}
				if (Geometry.LessThan(num4, xyz.Y))
				{
					num4 = xyz.Y;
				}
			}
			XYZ xyz2 = new XYZ(num, num2, 0.0);
			XYZ xyz3 = new XYZ(num3, num4, 0.0);
			XYZ pt = (xyz2 + xyz3) / 2.0;
			Element elemByUniqueId = CommonInterface.GetElemByUniqueId(doc, this.m_strHostUniqueId);
			if (elemByUniqueId == null)
			{
				return;
			}
			Floor floor = elemByUniqueId as Floor;
			if (floor == null)
			{
				return;
			}
			XYZ openingPosition = CommonInterface.GetOpeningPosition(pt, floor);
			if (openingPosition == null)
			{
				return;
			}
			OpeningProfileRect openingProfileRect = this.m_profile as OpeningProfileRect;
			openingProfileRect.m_position = openingPosition;
			openingProfileRect.m_cutWidth = xyz3.X - xyz2.X;
			openingProfileRect.m_cutHeight = xyz3.Y - xyz2.Y;
			openingProfileRect.m_directionProfile = XYZ.BasisZ;
		}

		public override void GetProfileOutLine3D(ref List<XYZ> lstOutLineView3DPt, ref Transform transform, bool isAddExtend, XYZ location = null)
		{
			List<XYZ> list = new List<XYZ>();
			this.m_profile.GetProfileOutLineOnXOY(ref list, isAddExtend);
			if (transform == null)
			{
				transform = Transform.CreateRotation(XYZ.BasisZ, 0.0);
				transform.BasisY = XYZ.BasisY;
				transform.BasisZ = XYZ.BasisZ;
				transform.BasisX = XYZ.BasisX;
			}
			transform.Origin = this.m_profile.m_position;
			foreach (XYZ xyz in list)
			{
				lstOutLineView3DPt.Add(transform.OfPoint(xyz));
			}
		}
	}
}
